package com.atguigu.sugar.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.sugar.bean.C3Bean;
import com.atguigu.sugar.bean.Kw;
import com.atguigu.sugar.bean.ProvinceBean;
import com.atguigu.sugar.bean.TrafficBean;
import com.atguigu.sugar.service.KwService;
import com.atguigu.sugar.service.ProvinceService;
import com.atguigu.sugar.service.TradeStatsService;
import com.atguigu.sugar.service.TrafficService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author lzc
 * @Date 2022/6/2 9:24
 */

@RestController
public class SugarController {
    
    @Autowired
    TradeStatsService tradeStatsService;
    
    @RequestMapping("/gmv")
    public String gmv(@RequestParam(value = "date" ,defaultValue = "0") int date) {
        
        if(date == 0){
            date = Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(System.currentTimeMillis()));
        }
    
        System.out.println(date);
    
    
        Double data = tradeStatsService.selectTotalAmount(date);
        
        JSONObject result = new JSONObject();
        result.put("status", 0);
        result.put("msg", "");
        result.put("data", data);
        
        return result.toJSONString();
    }
    
    @RequestMapping("/gmv/spu")
    public String gmvBySpu(Integer date) {
        
        List<Map<String, Object>> list = tradeStatsService.gmvBySpu(date);
        
        JSONObject result = new JSONObject();
        result.put("status", 0);
        result.put("msg", "");
        
        JSONObject data = new JSONObject();
        
        JSONArray categories = new JSONArray();
        
        for (Map<String, Object> map : list) {
            Object spuName = map.get("spu_name");
            categories.add(spuName);
        }
        
        data.put("categories", categories);
        
        
        JSONArray series = new JSONArray();
        JSONObject one = new JSONObject();
        one.put("name", "spu name");
        JSONArray data1 = new JSONArray();
        for (Map<String, Object> map : list) {
            Object orderAmount = map.get("order_amount");
            data1.add(orderAmount);
        }
        one.put("data", data1);
        
        
        series.add(one);
        
        data.put("series", series);
        
        result.put("data", data);
        
        
        return result.toJSONString();
    }
    
    
    @RequestMapping("/gmv/c3")
    public String gmvByC3(Integer date) {
        List<C3Bean> list = tradeStatsService.gmvByC3(date);
        
        JSONObject result = new JSONObject();
        result.put("status", 0);
        result.put("msg", "");
        
        JSONArray data = new JSONArray();
        
        for (C3Bean c3Bean : list) {
            JSONObject obj = new JSONObject();
            obj.put("name", c3Bean.getCategory3_name());
            obj.put("value", c3Bean.getOrder_amount());
            
            data.add(obj);
        }
        
        
        result.put("data", data);
        
        
        return result.toJSONString();
    }
    
    
    @Autowired
    ProvinceService provinceService;
    
    @RequestMapping("/province")
    public String province(Integer date) {
        
        List<ProvinceBean> list = provinceService.statsProvince(date);
        
        JSONObject result = new JSONObject();
        result.put("status", 0);
        result.put("msg", "");
        
        JSONObject data = new JSONObject();
        
        JSONArray mapData = new JSONArray();
        
        for (ProvinceBean provinceBean : list) {
            JSONObject obj = new JSONObject();
            
            obj.put("name", provinceBean.getProvince_name());
            obj.put("value", provinceBean.getOrder_amount());
            
            JSONArray tooltipValues = new JSONArray();
            tooltipValues.add(provinceBean.getOrder_count());
            obj.put("tooltipValues", tooltipValues);
            
            mapData.add(obj);
        }
        
        data.put("mapData", mapData);
        
        data.put("valueName", "限售额");
        
        JSONArray tooltipNames = new JSONArray();
        tooltipNames.add("订单数");
        data.put("tooltipNames", tooltipNames);
        
        JSONArray tooltipUnits = new JSONArray();
        tooltipUnits.add("个");
        data.put("tooltipUnits", tooltipUnits);
        
        result.put("data", data);
        
        
        return result.toJSONString();
    }
    
    @Autowired
    TrafficService trafficService;
    
    @RequestMapping("/traffic")
    public String traffic(Integer date) {
        
        List<TrafficBean> list = trafficService.statsByHour(date);
        
        Map<Integer, TrafficBean> hourToTrafficBeanMap = new HashMap<>();
        // 转成map集合:  hour ->TrafficBean
        for (TrafficBean trafficBean : list) {
            hourToTrafficBeanMap.put(trafficBean.getHour(), trafficBean);
        }
        
        
        JSONObject result = new JSONObject();
        result.put("status", 0);
        result.put("msg", "");
        
        JSONObject data = new JSONObject();
        JSONArray categories = new JSONArray();
        
        for (int i = 0; i < 24; i++) {
            categories.add(i);
        }
        
        data.put("categories", categories);
        
        JSONArray series = new JSONArray();
        
        JSONObject pv = new JSONObject();
        pv.put("name", "pv");
        JSONArray pvData = new JSONArray();
        pv.put("data", pvData);
        series.add(pv);
        
        JSONObject uv = new JSONObject();
        uv.put("name", "uv");
        JSONArray uvData = new JSONArray();
        uv.put("data", uvData);
        series.add(uv);
        
        JSONObject sv = new JSONObject();
        sv.put("name", "sv");
        JSONArray svData = new JSONArray();
        sv.put("data", svData);
        series.add(sv);
        
        
        
        
        for (int hour = 0; hour < 24; hour++) {
            TrafficBean bean = hourToTrafficBeanMap.getOrDefault(hour, new TrafficBean(hour, 0L, 0L, 0L));
            pvData.add(bean.getPv());
            uvData.add(bean.getUv());
            svData.add(bean.getSv());
        }
        data.put("series", series);
        
        result.put("data", data);
        
        
        return result.toJSONString();
    }
    
    
    @Autowired
    KwService kwService;
    
    @RequestMapping("/kw")
    public String kw(Integer date) {
    
        List<Kw> list = kwService.kw(date);
    
    
        JSONObject result = new JSONObject();
        result.put("status", 0);
        result.put("msg", "");
    
        JSONArray data = new JSONArray();
    
        for (Kw kw : list) {
            JSONObject obj = new JSONObject();
            obj.put("name", kw.getKeyword());
            obj.put("value", kw.getScore());
            data.add(obj);
        }
        
        result.put("data", data);
        
        
        return result.toJSONString();
    }
}
